# -*- coding: utf-8 -*-
import scrapy


class KuaiipSpider(scrapy.Spider):
    name = 'kuaiip'
    allowed_domains = ['www.kuaidaili.com']
    start_url = 'https://www.kuaidaili.com/free/inha/{0}/'
    max_page_size=5

    def start_requests(self):
        for i in range(1,self.max_page_size+1):
            yield scrapy.Request(self.start_url.format(i), callback=self.parse, dont_filter=True)

    def parse(self, response):
        results=response.xpath('/html/body/div/div[4]/div[2]/div/div[2]/table/tbody/tr')

        for record in results:
            fields=record.xpath('td/text()')

            proxy_item={'source':self.name,'is_china':True}

            proxy_item['ip']=fields[0].extract().strip()
            proxy_item['port']=fields[1].extract().strip()
            proxy_item['address']=fields[4].extract().strip()

            yield proxy_item